﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using MedicalSystem.SamrtReport.Library;
using System.Diagnostics;
using MedicalSystem.SamrtReport.Tools;
using System.Configuration;

namespace MedicalSystem.SamrtReport.Settings
{
    public partial class NewReportSettingFrm : Form
    {
        public NewReportSettingFrm()
        {
            InitializeComponent();
        }
        private bool _isWindowsFormSelected;
        private void InitalizeBindingDataComboBox()
        {
            
            string[] files = Directory.GetFiles(BindingData.Path);
            if (files.Length == 0)
            {
                Trace.WriteLine("提示,您当前设计器中无可用数据源,请通过数据源添加向导添加数据源到表单设计器!");
            }
            else
            {
                this.BindingDataComboBox.Items.Clear();

                foreach (string file in files)
                {
                    if (Path.GetExtension(file).ToLower() != ".xml")
                        continue;
                    string fileName = Path.GetFileNameWithoutExtension(file);
                    this.BindingDataComboBox.Items.Add(fileName);
                }

                if (BindingDataComboBox.Items.Count == 0)
                    Trace.WriteLine("提示,您当前设计器中无可用数据源,请通过数据源添加向导添加数据源到表单设计器!");

            }

            
        }
      
        private void NewReportSettingFrm_Load(object sender, EventArgs e)
        {
            InitalizeBindingDataComboBox();
           
            string defaultBindingSource = ConfigurationManager.AppSettings["defaultBindingSource"];
            if (defaultBindingSource != string.Empty)
            {
                if (this.BindingDataComboBox.Items.Contains(defaultBindingSource))
                {
                    this.BindingDataComboBox.Text = defaultBindingSource;
                    this.checkBox1.Checked = true;
                }
                else
                {
                    Trace.WriteLine(string.Format("当前数据源中并不存默认数据源<{0}>'", defaultBindingSource));
                }
            }
            string isWindowsFormReport = ConfigurationManager.AppSettings["isWindowsFormReport"];
            if (isWindowsFormReport != string.Empty)
            {
                if (Convert.ToBoolean(isWindowsFormReport) == true)
                   FormButton_Click(this, EventArgs.Empty);
               else
                   button1_Click(this, EventArgs.Empty);
               this.checkBox1.Checked = true;
            }
            CustomFormHepler.ShowForm(this);

        }
        private void button2_Click(object sender, EventArgs e)
        {
            DataSettingWizard dataSeeting = new DataSettingWizard();
            if (dataSeeting.ShowDialog() == DialogResult.OK)
            {
                InitalizeBindingDataComboBox();
            }
        }

        private void BindingDataComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.button3.Enabled = !string.IsNullOrEmpty(BindingDataComboBox.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(BindingDataComboBox.Text))
            {
                this.errorProvider1.SetError(BindingDataComboBox, "请选择表单绑定数据源");
                return;
            }
            if (this.checkBox1.Checked)
            {
                if (ConfigurationManager.AppSettings["defaultBindingSource"] != BindingDataComboBox.Text
                    || ConfigurationManager.AppSettings["isWindowsFormReport"] != this._isWindowsFormSelected.ToString())
                {
                   
                    try
                    {
                        Configuration configuration = ConfigurationManager.OpenExeConfiguration("SamrtReport.exe");
                        configuration.AppSettings.Settings["defaultBindingSource"].Value = BindingDataComboBox.Text;
                        configuration.AppSettings.Settings["isWindowsFormReport"].Value = _isWindowsFormSelected.ToString();
                        configuration.Save(ConfigurationSaveMode.Modified);
                        ConfigurationManager.RefreshSection("appSettings");
                    }
                    catch (Exception ex)
                    {

                        Trace.WriteLine(string.Format("未能设置默认绑定数据源为<{0}>，错误原因为:{1}", BindingDataComboBox.Text,ex.Message));
                    }

                    Trace.WriteLine(string.Format("已设置默认绑定数据源为<{0}>,默认新建表单类型为<{1}>",
                        BindingDataComboBox.Text, _isWindowsFormSelected ? "Windows Form" : "Mobile Form"));
                }
                
                 
            }
            BindingData.CurrentBindingSourceName = BindingDataComboBox.Text;
            this.DialogResult = DialogResult.OK;
           
        }

        private void button4_Click(object sender, EventArgs e)
        {

            this.DialogResult = DialogResult.Cancel;
        }

        private void FormButton_Click(object sender, EventArgs e)
        {
            _isWindowsFormSelected = true;
            this.textBox1.Text = "帮助您创建可在Windows平台下运作的表单";
            this.button3.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _isWindowsFormSelected = false;
            this.textBox1.Text = "帮助您创建可在Mobile移动设备平台下运作的表单";
            this.button3.Enabled = true;
        }

        public bool IsNewWindowsForm
        {
            get { return _isWindowsFormSelected; }
        }
    }
}
